package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Robot;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 获取机器人个人二维码3.0回调
 *
 * <pre>
 * {
 *     "nType": 4517,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcPersonQRCode": "",
 *         "vcQRCodeAddress": ""
 *     },
 *     "nTimeStamp": 1604384002367
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4517Handler implements CallbackHandler {

    @Resource
    private RobotMapper robotMapper;

    @Override
    public String name() {
        return "获取机器人个人二维码3.0回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4517;
    }

    @Override
    public void handle(Callback callback) {
        JSONObject data = (JSONObject) callback.getData();
        LambdaUpdateWrapper<Robot> wrapper = new LambdaUpdateWrapper<Robot>()
                .set(Robot::getQrcode, data.getString("vcPersonQRCode"))
                .set(Robot::getQrcodeAddress, data.getString("vcQRCodeAddress"))
                .eq(Robot::getId, callback.getRobotId())
                .set(Robot::getUpdatedTime, LocalDateTime.now());
        robotMapper.update(wrapper);
    }
}
